package com.sl.springboot.service.impl;

import com.sl.springboot.dao.CardDao;
import com.sl.springboot.model.Card;
import com.sl.springboot.model.PageInfo;
import com.sl.springboot.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import com.sl.springboot.util.RandomIdUtil;

/**
 * 用户Service接口实现类
 * @author: mty
 */
@Service("cardService")
@Transactional
public class CardServiceImpl implements CardService {

    @Autowired
    private CardDao cardDao;


    //分页查询
    @Override
    public PageInfo<Card> findPageInfo(Integer pageIndex, Integer pageSize,Map mp) {
        PageInfo<Card> pi = new PageInfo<Card>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        mp.put("currentPage",(pi.getPageIndex()-1)*pi.getPageSize());
        mp.put("pageSize",pi.getPageSize());
        //获取总条数
        Integer totalCount = cardDao.totalCount(mp);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //按条件查询
            List<Card> cardList =	cardDao.getCardList(mp);
            pi.setList(cardList);
        }
        return pi;
    }


    //添加
    @Override
    public int addCard(Card card) {
        card.setId(RandomIdUtil.getRandomIdByUUID());
        return cardDao.addCard(card);
    }


    //根据id删除
    @Override
    public int deleteCard(String id) {
        return cardDao.deleteCard(id);
    }


    //修改宿舍信息
    @Override
    public int updateCard(Card card) {
        return cardDao.updateCard(card);
    }

    //根据ID查询
    @Override
    public Card findCardById (String id){
        Card d = cardDao.findCardById(id);
        return  d;
    }

    //查询所有
    @Override
    public List<Card> getAll(){
        List<Card> cardList = cardDao.getAll();
        return cardList;
    }

    //按照条件查询
    @Override
    public List<Card> queryFilter(Map mp){
        List<Card> cardList = cardDao.queryFilter(mp);
        return cardList;
    }
}
